//
//  LittleBallViewController.m
//  LittleBall
//
//  Created by mac on 11-12-23.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "LittleBallViewController.h"
#import "LittleBallConfigViewController.h"
#import "LittleBallPlayViewController.h"

@implementation LittleBallViewController

@synthesize m_traditionalMode;
@synthesize m_timeMode;
@synthesize m_gameConfig;
@synthesize m_gameExit;

@synthesize flowerEmitter;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    BOOL result = false;
    m_gameManagement = [[LittleBallGameManagement alloc] initEx];
    
    // 初始化文件数据：(int)游戏难度、历练值、银两值、音乐音量、音效音量、（道具状态 未加）
    BOOL fileIndicator = [m_gameManagement ifFileExists];
    if (!fileIndicator)
    {
        result = [m_gameManagement WriteDataIntoFile];
    }
    else
    {
        result = [m_gameManagement readDataFromFile];        
    }    

    // 初始化所有音乐音效
    [m_gameManagement loadBackgroundMusicwithName:@"background" Type:@"mp3"];
    [m_gameManagement playBackgroundMusic];
    
    // 这里clearball声音要变成按钮声效和设置界面滚动生效
    [m_gameManagement loadSoundEffectwithName:@"clearball" fileType:@"WAV" goal:EFFECT_SOUND_DEFAULT];
    [m_gameManagement loadSoundEffectwithName:@"clearball" fileType:@"WAV" goal:EFFECT_SOUND_BUTTON];
    
    // flower
    [self flower];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    switch (interfaceOrientation) 
    {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            return true;
            
        default:
            break;
    }
    
    return false;
}

- (IBAction)onButton_traditionalMode:(id)sender
{
    //if (configurationPause != YES)  // 判断设置界面是否打开；打开的情况下不能触发事件
    {
        m_playViewController = [[LittleBallPlayViewController alloc] initWithNibName:@"LittleBallPlayView" bundle:nil];
        m_playViewController.m_gameManagement = m_gameManagement;
        
        [m_playViewController setGameMode:TRADITONAL_MODE];
        //[m_playViewController setGameDifficulty:m_gameDifficultyInRootView];
        [m_playViewController initializePlayView];
        [m_playViewController setGameConfiguration];
        [self.view addSubview:m_playViewController.view];
        [m_playViewController startGame]; 
    }
}

- (IBAction)onButton_timeMode:(id)sender
{    
    //if (configurationPause != YES)
    {
        m_playViewController = [[LittleBallPlayViewController alloc] initWithNibName:@"LittleBallPlayView" bundle:nil]; 
        m_playViewController.m_gameManagement = m_gameManagement;
        
        [m_playViewController setGameMode:SURVIVAL_MODE];
        //[m_playViewController setGameDifficulty:m_gameDifficultyInRootView];
        [m_playViewController initializePlayView];
        [m_playViewController setGameConfiguration];
        [self.view addSubview:m_playViewController.view];
        [m_playViewController startGame];  
    }
}


- (IBAction)onButton_gameConfig:(id)sender
{    
    //configurationPause = YES;
    
    m_configViewController = [[LittleBallConfigViewController alloc] initWithNibName:@"LittleBallGameConfigView" bundle:nil];
    m_configViewController.m_gameManagement = m_gameManagement;
    
    m_gameManagement.m_viewChangeAnimationDelegate = [m_gameManagement.m_viewChangeAnimationDelegate initWithFrom:self To:m_configViewController ChangeType:LB_VIEW_CHANGE_MAIN_TO_CONFIG];
    
}

- (void)hiddenButtons:(BOOL)bHidden
{
    m_traditionalMode.hidden = bHidden;
    m_timeMode.hidden = bHidden;
    m_gameConfig.hidden = bHidden;
    m_gameExit.hidden = bHidden;
}

- (void)removeConfigViewAnimation
{    
     m_gameManagement.m_viewChangeAnimationDelegate = [m_gameManagement.m_viewChangeAnimationDelegate initWithFrom:m_configViewController To:self ChangeType:LB_VIEW_CHANGE_CONFIG_TO_MAIN];
}

- (IBAction)onButton_gameExit:(id)sender
{
    //if (configurationPause != YES)
    {
        [m_gameManagement WriteDataIntoFile];
        
        exit(1);
    }
}

/*
- (void)setConfigurationPause:(BOOL)input
{
    configurationPause = input;
}
*/

- (void)viewDidUnload
{    
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}


-(void)flower
{
    // Configure the particle emitter to the top edge of the screen
	flowerEmitter = [CAEmitterLayer layer];
	flowerEmitter.emitterPosition = CGPointMake(self.view.bounds.size.width / 2.0, -30);
	flowerEmitter.emitterSize		= CGSizeMake(self.view.bounds.size.width * 2.0, 0.0);;
	
	// Spawn points for the flakes are within on the outline of the line
	flowerEmitter.emitterMode		= kCAEmitterLayerOutline;
	flowerEmitter.emitterShape	= kCAEmitterLayerLine;
	
	// Configure the snowflake emitter cell
	CAEmitterCell *flowerflake = [CAEmitterCell emitterCell];
	
	flowerflake.birthRate		= 1.0;
	flowerflake.lifetime		= 120.0;
	
	flowerflake.velocity		= -10;				// falling down slowly
	flowerflake.velocityRange = 10;
	flowerflake.yAcceleration = 2;
	flowerflake.emissionRange = 0.5 * M_PI;		// some variation in angle
	flowerflake.spinRange		= 0.25 * M_PI;		// slow spin
	
	flowerflake.contents		= (id) [[UIImage imageNamed:@"flower1_t"] CGImage];
	flowerflake.color			= [[UIColor colorWithRed:0.600 green:0.658 blue:0.743 alpha:1.000] CGColor];
    
	// Make the flakes seem inset in the background
	flowerEmitter.shadowOpacity = 1.0;
	flowerEmitter.shadowRadius  = 0.0;
	flowerEmitter.shadowOffset  = CGSizeMake(0.0, 1.0);
	flowerEmitter.shadowColor   = [[UIColor whiteColor] CGColor];
	
	// Add everything to our backing layer below the UIContol defined in the storyboard
	flowerEmitter.emitterCells = [NSArray arrayWithObject:flowerflake];
	//[self.view.layer insertSublayer:snowEmitter atIndex:0];
    [m_frontView.layer insertSublayer:flowerEmitter atIndex:0];
}


@end
